<?php

/**
 * 默认控制器
 */
class Controller_Jqueryplugin extends Controller_Abstract
{
	function actionIndex()
	{
		$page = $this->_context->get('page', 1);
		$type_id = $this->_context->get('type_id', null);
    	
    	$jqueryPlugins = Jqueryplugins::model()->paginationGetAll($page, $type_id);
    	$pagination = new Pagination($jqueryPlugins['totalCount'], url($this->_context->requestUDI(false), array('type_id' => $type_id)), $page, JQUERYPLUGINS_LIST_PERPAGE);
		unset($jqueryPlugins['totalCount']);
    	$this->_view['jqueryPlugins'] = $jqueryPlugins;
    	$this->_view['pagination'] = $pagination->render();
    	
    	$this->_view['type_id'] = $type_id;
    	
		$this->_view['beusedJqueryTypes'] = Types::model()->find('*',
			array('id' => Jqueryplugins::model()->find('type_id', null, array('distinct' => true, 'stripName' => true)))
		);
		
		$this->_view['crumbs'] = array(
			'Jquery插件' => ''
		);
	}
	
    function actionDetail()
    {
        $id = $this->_context->get('id', null);
    	if($id){
    		$jqueryPluginsModel = new jqueryPlugins();
    		$jqueryPlugin = $jqueryPluginsModel->find('*', array('id' => $id), array('onlyOne' => true, 'replaceDescriptionVars' => true, 'htmlSpecialcharsPreContent' => true));
    		
    		if(file_exists(iconv('UTF-8', 'GB2312', JQUERYPLUGINS_DIR . DIRECTORY_SEPARATOR . $jqueryPlugin['root_foldername'] . DIRECTORY_SEPARATOR . $jqueryPlugin['download_pack']))){
	    		$this->_view['download_pack'] = $jqueryPlugin['download_pack'];
	    	}
	    	
	    	$this->_view['crumbs'] = array(
				'Jquery插件' => url('jqueryplugin/index'),
	    		$jqueryPlugin['name'] => ''
			);
    		
			$relateds = $jqueryPluginsModel->find('*', array('type_id' => $jqueryPlugin['type_id']), array('limit' => 5), array('id != ?'), array($id));
    		$this->_view['jqueryPlugin'] = $jqueryPlugin;
    		$this->_view['relateds'] = $relateds;
    	}
    	if(empty($jqueryPlugin)){
    		die('没找到相应ID的Jqueryplugin模板,跳转到404页面');
    		exit();
    	}
    	
    }
    
    protected function _after_execute(& $response)
    {
    	$this->_view['recommends'] = Jqueryplugins::model()->getRecommends();
    	$this->_view['downloads'] = Jqueryplugins::model()->getTopDownloads();
    	$this->_view['vieweds'] = Jqueryplugins::model()->getTopViews();
    	
    	parent::_after_execute($response);
    }
}

